package dexter.net.unit;

import static org.junit.Assert.*;

import java.util.List;
import java.util.Vector;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import dexter.core.host.HostAddress;


public class DummyTest {

	private DummyNode node1, node2;
	private List<DummyNode> nodes;
	private String context; 
	
	@Before
	public void init() {
		
		context = "test";		
		
		HostAddress hh1 = new HostAddress("localhost",5555);
		node1 = new DummyNode(context, hh1);
		
		HostAddress hh2 = new HostAddress("localhost",6666);
		node2 = new DummyNode(context, hh2);
		
		nodes = new Vector<DummyNode>(10);
		for (int i=0; i<10;i++) {
			DummyNode dn = new DummyNode(context, new HostAddress("localhost",7000+i));
			nodes.add(dn);
		}
		
	}

	@Test
	@Ignore
	public void testTwoNodes(){
		
		DummyNode.reset();
		
		try {
		
		  Thread.sleep(1000);
			
		  //send h1 -> h2
		  System.out.println("Test Host1 -> Host2");
		  System.out.println("Start");
		  
		  HostAddress hh1 = node1.getHostAddress();
		  node2.sayHello(hh1,0);
		  
		  System.out.println("End");
		  
		  //Thread.sleep(3000);
		  
		  
		  //send h2 -> h1
		  System.out.println("Test Host2 -> Host1");
		  System.out.println("Start");
		  
		  HostAddress hh2 = node2.getHostAddress();
		  node1.sayHello(hh2,1);
		  
		  System.out.println("End");
		  
		  //Thread.sleep(3000);
		  
		} catch(Exception e) {
			e.printStackTrace();
			assertTrue(false);
		}
		
		assertTrue(true);
		System.exit(0);
	}
	
	@Test
	public void testSeveralNodes(){
		
		DummyNode.reset();
		
		int numMsgs = 200;
		
		try {
		
		Thread.sleep(1000);
		   
	    for (int i=0;i<numMsgs;i++) {
	    	
	    	int n1 = (int) (nodes.size()*Math.random());
	    	DummyNode dn1 = nodes.get(n1);
	    	
	    	int n2 = (int) (nodes.size()*Math.random());
	    	DummyNode dn2 = nodes.get(n2);
	    	
	    	dn1.sayHello(dn2.getHostAddress(),i);
	    }
			
		
			
		} catch(Exception e) {
			e.printStackTrace();
			assertTrue(false);
		}
		
		assertEquals(new Integer(numMsgs), new Integer(DummyNode.getCount()));

		System.out.println("Finish");
		System.exit(0);
	}
}
